package com.java.ttt.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.java.ttt.bo.GameRoom;
import com.java.ttt.bo.User;
import com.java.ttt.common.TTTConstants;
import com.java.ttt.dao.WelcomeDao;
import com.opensymphony.xwork2.ActionSupport;

public class Welcome extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String password;
	
	private ArrayList<GameRoom> openGameRooms;
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		WelcomeDao dao = new WelcomeDao();
		User user = dao.getuserDetails(username, password);
		
		if (user == null){
			addActionError(getText("invalid.username.password"));
			return LOGIN;
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute(TTTConstants.LOGGED_IN_USER, user);
		
		openGameRooms = dao.getOpenGameRoomsList();
		
		return SUCCESS;
	}
	
	// simple validation
	public void validate() {
		if ("".equals(getUsername())) {
			addFieldError("username", getText("username.required"));
		}
		if ("".equals(getPassword())) {
			addFieldError("password", getText("password.required"));
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public ArrayList<GameRoom> getOpenGameRooms() {
		return openGameRooms;
	}

	public void setOpenGameRooms(ArrayList<GameRoom> openGameRooms) {
		this.openGameRooms = openGameRooms;
	}
}
